﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using VOL.Core.Extensions;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.EquipmentManage;
using Microsoft.EntityFrameworkCore;

namespace VOL.SmartMecial.Services.EquipmentManage
{
    public class EquipmentServices : IEquipmentServices
    {
        private readonly IBaseRepository<Entity.DomainModels.Equipment> _equipmentRep;
        private readonly IBaseRepository<Entity.DomainModels.Office> _officeRep;
        private readonly IBaseRepository<Entity.DomainModels.WardArea> _wardareaRep;
        private readonly IBaseRepository<Entity.DomainModels.NursingUnit> _nursingUnitRep;
        private readonly IBaseRepository<Entity.DomainModels.WardAreaNursingUnit> _wardAreaNursingUnitRep;

        public EquipmentServices(IBaseRepository<Entity.DomainModels.Equipment> equipmentRep, IBaseRepository<Entity.DomainModels.Office> officeRep, IBaseRepository<Entity.DomainModels.WardArea> wardareaRep, IBaseRepository<Entity.DomainModels.NursingUnit> nursingUnitRep, IBaseRepository<Entity.DomainModels.WardAreaNursingUnit> wardAreaNursingUnitRep)
        {
            _equipmentRep = equipmentRep;
            this._officeRep = officeRep;
            this._wardareaRep = wardareaRep;
            _nursingUnitRep = nursingUnitRep;
            _wardAreaNursingUnitRep = wardAreaNursingUnitRep;
        }

        /// <summary>
        /// 科室树
        /// </summary>
        /// <returns></returns>
        public async Task<List<OfficeTree>> ShowTree()
        {
            var tree = new List<OfficeTree>();

            // 获取一级科室
            var firstLevelOffices = await _officeRep.GetAll()
                .Where(x => x.ParentDepart == 0)
                .ToListAsync();

            foreach (var off in firstLevelOffices)
            {
                var node = new OfficeTree
                {
                    Id = off.Id,
                    Name = off.OfficeName,
                    Code = off.OfficeCode,
                    ParentId = 0,
                    Level = 1,
                    NodeType = "department"
                };

                // 获取二级科室
                var secondLevelOffices = await _officeRep.GetAll()
                    .Where(x => x.ParentDepart == off.Id)
                    .ToListAsync();

                foreach (var special in secondLevelOffices)
                {
                    var secondnode = new OfficeTree
                    {
                        Id = special.Id,
                        Name = special.OfficeName,
                        Code = special.OfficeCode,
                        ParentId = off.Id,
                        Level = 2,
                        NodeType = "department"
                    };

                    // 获取相关病区
                    var relatedWards = await _wardareaRep.GetAll()
                        .Where(x => x.OfficeCode == special.OfficeCode)
                        .ToListAsync();

                    foreach (var ward in relatedWards)
                    {
                        var wardnode = new OfficeTree
                        {
                            Id = ward.Id,
                            Name = ward.Name,
                            ParentId = special.Id,
                            Level = 3,
                            NodeType = "ward"
                        };

                        // 获取相关护理单元
                        var nursingUnitCodes = await _wardAreaNursingUnitRep.GetAll()
                            .Where(x => x.WardAreaCode == ward.WardAreaCode)
                            .Select(x => x.NursingUnit)
                            .ToListAsync();

                        var relatedNursingUnits = await _nursingUnitRep.GetAll()
                            .Where(x => nursingUnitCodes.Contains(x.NursingUnitCode))
                            .ToListAsync();

                        foreach (var unit in relatedNursingUnits)
                        {
                            var unitnode = new OfficeTree
                            {
                                Id = unit.Id,
                                Name = unit.Name,
                                ParentId = ward.Id,
                                Level = 4,
                                NodeType = "nursingUnit"
                            };
                            wardnode.Children.Add(unitnode);
                        }
                        secondnode.Children.Add(wardnode);
                    }
                    node.Children.Add(secondnode);
                }
                tree.Add(node);
            }
            return tree;
        }

        /// <summary>
        /// 显示设备登记
        /// </summary>
        /// <param name="nursingUnitId"></param>
        /// <returns></returns>
        public async Task<List<Entity.DomainModels.Equipment>> ShowEquipment(int? nursingUnitId)
        {
            var equipmentList = _equipmentRep.GetAll();
            equipmentList = equipmentList.WhereIF(nursingUnitId != null, m => m.NursingUnitId == nursingUnitId);
            return equipmentList.ToList();
        }

        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public async Task<int> AddEquipment(Entity.DomainModels.Equipment equipment)
        {
            var result = await _equipmentRep.AddAsync(equipment);
            return result;
        }

        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelEquipment(int id)
        {
            var result = await _equipmentRep.DelAll(m => m.Id == id);
            return result;
        }

        /// <summary>
        /// 回显设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Entity.DomainModels.Equipment> GetEquipmentById(int id)
        {
            var result = await _equipmentRep.FindAsync(id);
            return result;
        }

        /// <summary>
        /// 编辑设备
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public async Task<int> UpdateEquipment(Entity.DomainModels.Equipment equipment)
        {
            var result = await _equipmentRep.UpdateAsync(equipment);
            return result;
        }
    }
}
